package com.bawei.module_home.ui.area

import android.util.Log
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.adapter.AreaAdapter
import com.bawei.module_home.databinding.ActivityAreaBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.AreaModelItem
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue

@AndroidEntryPoint
@Route(path = Router.AREA_PAGE)
class AreaActivity : BaseActivity<HomeServiceViewModel,  ActivityAreaBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_area

    val areaAdapter: AreaAdapter by lazy {
        AreaAdapter()
    }
    private val mViewModel: HomeServiceViewModel by viewModels()



    override fun initView() {
        super.initView()
        initAdapter()
        mBinding.areaToobar.apply {
            title.text="车位管理"
            more.setImageResource(R.drawable.baseline_add_24)
            more.setOnClickListener({
                ARouter.getInstance().build(Router.ADD_AREA_PAGE).navigation()
            })
            back.setOnClickListener({
                finish()
            })
        }
        areaAdapter.setOnItemClickListener(object : AreaAdapter.OnItemClickListener{
            override fun clickListener(
                item: AreaModelItem,
                position: Int
            ) {
                ARouter.getInstance().build(Router.AREA_DETAIL_PAGE).withInt("areaId", item.id).navigation()
            }

        })

        mBinding.search.setOnQueryTextListener(
            object : androidx.appcompat.widget.SearchView.OnQueryTextListener
            {
            override fun onQueryTextSubmit(query: String): Boolean {
                searchAreaSpaces(query)
                return true
            }

            override fun onQueryTextChange(newText: String): Boolean {
                searchAreaSpaces(newText)
                return true
            }

        })




    }

    fun searchAreaSpaces(query: String){
        if(TextUtils.isEmpty(query)){
            areaAdapter.setData(mViewModel.areaList)
        }else{
            mViewModel.areaNewList.clear()
            var lowerCaseQuery=query.toLowerCase()
            for (modelItem in mViewModel.areaList) {
                if(modelItem.spaceNumber.contains(lowerCaseQuery)){
                    mViewModel.areaNewList.add(modelItem)
                }
            }
            areaAdapter.setData(mViewModel.areaNewList)
        }

    }



    override fun initData() {
        mViewModel.getArea(1)

    }

    override fun initListener() {
        mViewModel.areaState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    areaAdapter.setData(mViewModel.areaList)
                }
            }
        }

        areaAdapter.setOnItemLongClickListener(object : AreaAdapter.OnItemLongClickListener{
            override fun LongClickListener(
                item: AreaModelItem,
                position: Int
            ) {
                mViewModel.deleteAreaState.observeWithLifecycle(this@AreaActivity){
                    when(it){
                        Empty -> {}
                        is Failed -> {
                            Log.i("GZY", "LongClickListener: "+it.message)
                        }
                        Loading -> {}
                        Success -> {
                            mViewModel.deleteArea(mViewModel.areaList[position].id)
                            areaAdapter.list.removeAt(position)
                            areaAdapter.notifyItemRemoved(position)
                            ToastUtils.showLong(this@AreaActivity,"删除成功")
                        }
                    }
                }

            }

        })



    }

    private fun initAdapter(){
        mBinding.areaRv.layoutManager= LinearLayoutManager(this)
        mBinding.areaRv.adapter=areaAdapter
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getArea(1)
    }

}